package com.jugheadzhou.emrpoc.controller;

import com.alibaba.fastjson.JSON;
import com.jugheadzhou.emrpoc.common.Result;
import com.jugheadzhou.emrpoc.dto.MedicalRecordContentDto;
import com.jugheadzhou.emrpoc.dto.MedicalRecordContentPublishDto;
import com.jugheadzhou.emrpoc.service.MedicalRecordContentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * @author jugheadzhou
 * @date 2021-10-24
 */
@Slf4j
@RestController
@RequestMapping("/mrContent")
public class MedicalRecordContentController {

    @Resource
    private MedicalRecordContentService medicalRecordContentService;

    /**
     * 病历内容查询
     *
     * @param recordId 病历记录ID
     * @return
     */
    @GetMapping("info")
    public Result<?> info(String recordId) {
        return Result.success(medicalRecordContentService.info(recordId));
    }

    /**
     * 病历内容保存
     *
     * @param contentDto 保存的病历内容对象
     * @return
     */
    @PostMapping("save")
    public Result<?> save(@Valid @RequestBody MedicalRecordContentDto contentDto, BindingResult br) {
        log.info("病历内容保存开始：{}", JSON.toJSONString(contentDto));
        if (br.hasFieldErrors()) {
            return Result.failed(br.getFieldError().getDefaultMessage());
        }
        return Result.success(medicalRecordContentService.save(contentDto));
    }

    /**
     * 病历内容完成/取消完成
     *
     * @param publishDto 完成的病历内容对象
     * @return
     */
    @PostMapping("complete")
    public Result<?> complete(@Valid @RequestBody MedicalRecordContentPublishDto publishDto) {
        log.info("病历内容完成/取消完成开始：{}", JSON.toJSONString(publishDto));
        return Result.success(medicalRecordContentService.complete(publishDto));
    }

    /**
     * 病历内容打印
     *
     * @param contentDto 打印的病历内容对象
     * @return
     */
    @PostMapping("print")
    public Result<?> print(@Valid @RequestBody MedicalRecordContentDto contentDto) {
        log.info("病历内容打印开始：{}", JSON.toJSONString(contentDto));
        return Result.success(medicalRecordContentService.print(contentDto));
    }
}
